package results;

import com.hp.gagawa.java.Node;
import com.hp.gagawa.java.elements.Table;
import com.hp.gagawa.java.elements.Td;
import com.hp.gagawa.java.elements.Th;
import com.hp.gagawa.java.elements.Tr;

import java.util.HashMap;

import static queped.quepedconstants.Strings.*;

/**
 * Created by:  Jason Ye
 * Date:        24/06/2012
 * Time:        10:54
 */
public class ResultsTableFactory {


    public static Table newResultsForRelation(Table table, String tableName, HashMap<String, HashMap<String, String>> resultsForRelation) {

        boolean isFirst = true;
        for (String transactionName : resultsForRelation.keySet()) {

            HashMap<String, String> resultsForTransaction = resultsForRelation.get(transactionName);

            table = populateResultsForTransaction(table, tableName, transactionName, resultsForTransaction, resultsForRelation.size(), isFirst);

            isFirst = false;
        }

        return table;
    }

    private static Table populateResultsForTransaction(Table table, String tableName, String transactionName, HashMap<String, String> resultsForTransaction, int numTransactions, boolean first) {

        String numCustomers = resultsForTransaction.get(NUMBER_OF_CUSTOMERS);
        String residenceTime = resultsForTransaction.get(RESIDENCE_TIME);
        String queueTime = resultsForTransaction.get(QUEUE_TIME);
        String util = resultsForTransaction.get(UTILISATION);
        String tps = resultsForTransaction.get(THROUGHPUT);
        String responseTime = resultsForTransaction.get(RESPONSETIME);

        Tr result = new Tr();

        if (first) {
            result.appendChild(newTd(tableName, numTransactions));
        }

        result.appendChild(newTd(transactionName));
        result.appendChild(newTd(numCustomers));
        result.appendChild(newTd(queueTime));
        result.appendChild(newTd(residenceTime));
        result.appendChild(newTd(util));
        result.appendChild(newTd(tps));
        result.appendChild(newTd(responseTime));

        table.appendChild(result);

        return table;
    }

    private static Td newTd(String contents, int height) {
        Td transactionTd = new Td();
        if (height > 1) {
            transactionTd.setRowspan(Integer.toString(height));
        }
        transactionTd.appendText(contents);
        return transactionTd;
    }

    public static Table newTableSpecificResultsTable() {

        Table result = new Table();

        String[] headers= {TABLE_NAME,TRANSACTION_NAME,NUMBER_OF_CUSTOMERS,QUEUE_TIME,RESIDENCE_TIME,UTILISATION,THROUGHPUT,RESPONSETIME};

        result.appendChild(generateTableHeaderRow(headers));

        return result;
    }
    
    public static Table systemResultsTable(){
        
        Table systemResults = new Table();

        systemResults.setBorder("5");

        String[] headers = {TRANSACTION_NAME,SYSTEM_THROUGHPUT,SYSTEM_RESPONSE_TIME};

        systemResults.appendChild(generateTableHeaderRow(headers));

        return systemResults;
        
    }

    public static Table newSystemResultsForRelation(Table systemResultsTable, String relation, HashMap<String, String> systemResultsForRelation) {

        Tr relationResult = new Tr();
        String sysTPS = systemResultsForRelation.get(SYSTEM_THROUGHPUT);
        String sysRT = systemResultsForRelation.get(SYSTEM_RESPONSE_TIME);

        relationResult.appendChild(newTd(relation));
        relationResult.appendChild(newTd(sysTPS));
        relationResult.appendChild(newTd(sysRT));

        systemResultsTable.appendChild(relationResult);

        return systemResultsTable;
    }

    private static Tr generateTableHeaderRow(String[] headers){

        Tr headerRow = new Tr();

        for(int i =0;i<headers.length;i++){
            headerRow.appendChild(newTh(headers[i]));
        }

        return headerRow;

    }

    private static Th newTh(String contents){
        Th header = new Th();
        header.appendText(contents);
        return header;
    }

    private static Td newTd(String contents) {

        return newTd(contents, 1);
    }


}
